<?php
namespace Home\Controller;
use Think\Controller;

class GrandController extends CommonController {
	public function  index(){
		$this -> assign('$title','会员等级管理');
		$userMod = M('user_type');	
		$user_type = $userMod -> where("id = '%d' ",$_SESSION['users']['user_type'])->find();

		$type = $userMod ->select();
		$this -> assign('type',$type);

		$this -> assign('user_type',$user_type);
		//用户升级日志
		$logMod = M('account_log');
		$user_update_log = $logMod -> field('change_desc,about_user_name,user_money2,change_time') ->where("user_id = '%d' and change_type='%s' ",$_SESSION['users']['user_id'],'70')->select();
		
		$this -> assign('user_update_log',$user_update_log);
		$this -> display('');
	}

	//升级用户等级
	public function update(){
		$userMod = M('users');
		$userMod -> startTrans();
			
				if($_SESSION['users']['user_money2']>=I('post.jg')*100){
					
					$_SESSION['users']['user_type'] = I('post.jg');
					if(!$_SESSION['users']['user_type'] == I('post.jg')){
						$_SESSION['users']['user_money2'] = $_SESSION['users']['user_money2']-I('post.jg')*100;
					}

					$res = $userMod -> where("user_id = '%s' ", $_SESSION['users']['user_id'])->save($_SESSION['users']);
					//添加日志
					$logMod = M('account_log');
					$log_update['user_id'] = $_SESSION['users']['user_id'];
					$log_update['user_money2'] = -I('post.jg')*100;
					$log_update['change_type'] = '70';
					$log_update['change_desc'] = '会员升级：VIP1->VIP2';
					$log_update['change_time'] = time();
					
					$res2 = $logMod ->add($log_update);
					if($res>0 && $res2>0){
						$userMod -> commit();
						$this -> success('升级成功,请稍等....',U('home/grand/index'));
					}else{
						$userMod -> rollback();
						$this -> error('升级失败,请核对信息后再次申请',U('home/grand/index'));
					}
					
				}else{
					$this -> error('抱歉,您的注册积分不足',U('home/grand/index'));
				}
			
				
	}

	public function updateAjax(){
		if(md5(I('post.repassword')) == $_SESSION['users']['repassword']){
			echo '1';
		}else{
			echo '2';
		}
	}
	public function downAjax(){
		if(md5(I('post.repassword')) == $_SESSION['users']['repassword']){
			echo '1';
		}else{
			echo '2';
		}
	}
	public function down(){
		$_SESSION['users']['user_type'] = I('post.jjg');
		$userMod = M('users');
		$res = $userMod -> where("user_id = '%s' ", $_SESSION['users']['user_id'])->save($_SESSION['users']);
		if($res>0){
					
						$this -> success('降级成功,请稍等....',U('home/grand/index'));
					}else{
						$userMod -> rollback();
					
					}
	}






}

